package qreasymarket.movil;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class TarifaPestana extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tarifa_pestana);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menuprincipal, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.PromocionService) {
			startActivity(new Intent(TarifaPestana.this, MenuCarrusel.class));
			return (true);
		}

		return (super.onOptionsItemSelected(item));
	}

	public void btnbuscar(View view) {

		final EditText txtBuscarDistrito = (EditText) findViewById(R.id.buscar_t);
		final TableLayout tlTarifas = (TableLayout) findViewById(R.id.tlTarifas);

		String texto = txtBuscarDistrito.getText().toString();

		RestClient cliente = new RestClient(
				"http://192.168.1.176:8080/QR-EasyMarket_Web/rest/tarifa_buscar");
		cliente.AddParam("distrito", texto);

		try {

			cliente.Execute(RequestMethod.GET);
			String jsontext = cliente.getResponse();
			Log.i("TarifaPestana", jsontext);

			JSONArray entries = new JSONArray(jsontext);

			String x = "";
			tlTarifas.removeAllViews();

			int i;

			for (i = 0; i < entries.length(); i++) {

				JSONObject post = entries.getJSONObject(i);
				x += "Distrito:" + post.getString("distrito") + " " + "Monto:"
						+ post.getDouble("monto") + "\n";
				TableRow tr = new TableRow(getBaseContext());

				TextView valueTV = new TextView(getBaseContext());
				valueTV.setText(post.getString("distrito"));
				valueTV.setTextColor(Color.BLACK);

				TextView value2 = new TextView(getBaseContext());
				value2.setText(post.getString("monto"));
				value2.setTextColor(Color.BLACK);

				TextView n = new TextView(getBaseContext());
				n.setText("   |          S/.");
				n.setTextColor(Color.BLACK);

				tr.addView(valueTV);
				tr.addView(n);
				tr.addView(value2);
				tlTarifas.addView(tr);

			}
			Log.i("TarifaBuscarActivity", x);

		} catch (Exception e) {
			Log.i("TarifaBuscarActivity", ":" + e.getMessage());
		}

	}
}
